package cn.dz.controller;

import cn.dz.entity.Employee;
import cn.dz.service.EmployeeService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@Controller
@RequestMapping("employee")
public class EmployeeController {
    private static final Logger log = LoggerFactory.getLogger(EmployeeController.class);

    private EmployeeService employeeService;

    @Value("${photo.file.dir}")
    private String realPath;

    @Autowired
    public EmployeeController(EmployeeService employeeService) {
        this.employeeService = employeeService;
    }

    /**
     * 删除员工
     * @param id
     * @return
     */
    @GetMapping("delete")
    public String delete(Integer id) {
        log.debug("删除的员工:{}", id);
        Employee employee = employeeService.findById(id);
        // 1.删除数据
        employeeService.delete(id);
        // 2.删除头像
        String photopath = employee.getPhotopath();
        File file = new File(realPath, photopath);
        if (file.exists()) {
            file.delete();
        }
        return "redirect:/employee/lists";
    }

    /**
     * 更新员工信息
     * @return
     */
    @RequestMapping("update")
    public String update(Employee employee, MultipartFile img) throws IOException {
        log.debug("更新之后员工信息: id:{},姓名:{},工资:{},生日:{}", employee.getId(), employee.getName(), employee.getSalary(), employee.getBirthday());
        //1.判断是否更新头像
        boolean notEmpty = !img.isEmpty();
        log.debug("是否更新头像: {}", notEmpty);
        if (notEmpty) {
            // 1.删除老的头像 根据id查询原始头像 2.处理新的头像上传
            String oldPhoto = employeeService.findById(employee.getId()).getPhotopath();
            File file = new File(realPath, oldPhoto);
            // 2.处理新的头像上传
            String originalFilename = img.getOriginalFilename();
            String newFileName = uploadPhoto(img, originalFilename);
            // 3.修改员工新的头像名称
            employee.setPhotopath(newFileName);
            if (file.exists()) {
                file.delete();
            }
        }
        // 没有更新头像直接更新基本信息
        employeeService.update(employee);
        return "redirect:/employee/lists"; //保存成功跳转列表页面
    }

    // 上传头像方法
    private String uploadPhoto(MultipartFile img, String originalFilename) throws IOException {
        String fileNamePrefix = new SimpleDateFormat("yyyyMMddHHmmssSSS").format(new Date());
        String fileNameSuffix = img.getOriginalFilename().substring(originalFilename.lastIndexOf("."));
        String newFileName = fileNamePrefix + fileNameSuffix;
        img.transferTo(new File(realPath, newFileName));
        return newFileName;
    }

    /**
     * 根据id查询员工信息
     * @param id
     * @param model
     * @return
     */
    @RequestMapping("detail")
    public String detail(Integer id, Model model) {
        log.debug("当前查询员工id: {}", id);
        //1.根据id查询一个
        Employee employee = employeeService.findById(id);
        if (employee == null) {
            log.warn("员工id={} 在数据库中不存在", id);
            return "redirect:/error";  // 可以重定向到一个错误页面
        }
        model.addAttribute("employee", employee);

        return "updateEmp"; // 跳转到更新页面
    }

    /**
     * 保存员工信息
     * 文件上传: 1.表单提交方式必须是post
     *          2.表单enctype属性必须为  multipart/form-data
     * @return
     */
    @PostMapping("save")
    public String save(Employee employee, MultipartFile img) throws IOException {
        log.debug("姓名:{}, 薪资:{}, 生日:{}", employee.getName(), employee.getSalary(), employee.getBirthday());
        String originalFilename = img.getOriginalFilename();
        log.debug("头像名称: {}", img.getOriginalFilename());
        log.debug("头像大小: {}", img.getSize());
        log.debug("上传的路径: {}", realPath);
        //1.处理头像的上传 & 修改文件名称
        String fileNamePrefix = new SimpleDateFormat("yyyyMMddHHmmssSSS").format(new Date());
        String fileNameSuffix = originalFilename.substring(originalFilename.lastIndexOf("."));
        String newFileName = fileNamePrefix + fileNameSuffix;
        img.transferTo(new File(realPath, newFileName));

        //2.保存员工信息
        employee.setPhotopath(newFileName);
        employeeService.save(employee);

        return "redirect:/employee/lists"; //保存成功跳转列表页面
    }

    /**
     * 员工列表
     * @return
     */
    @RequestMapping("lists")
    public String lists(Model model) {
        log.debug("查询所有员工信息");
        List<Employee> employeeList = employeeService.lists();
        model.addAttribute("employeeList", employeeList);
        return "emplist";
    }
}
